package com.sais.manage.boot.platform.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sais.common.entity.Result;
import com.sais.manage.boot.platform.annotation.Throttle;
import com.sais.manage.boot.platform.dto.RoleDTO;
import com.sais.manage.boot.platform.dto.RoleMenuDTO;
import com.sais.manage.boot.platform.dto.RoleSaveDTO;
import com.sais.manage.boot.platform.dto.RoleSearchDTO;
import com.sais.manage.boot.platform.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 宿舍管理系统-角色 controller 实现类
 *
 * @author zhang
 * @date 2019/11/30
 **/
@Api(tags = "角色管理")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/platform/role")
public class RoleController {

    @Resource
    private IRoleService roleService;

    @ApiOperation("角色 添加")
  //  @Idempotent
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result saveRole(@RequestBody RoleSaveDTO roleSaveDTO) {
        roleService.saveRole(roleSaveDTO);
        return new Result();
    }

    @ApiOperation("新建 角色")
   // @Idempotent
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Result createRole(@RequestBody RoleSaveDTO roleSaveDTO) {
        roleService.createRole(roleSaveDTO);
        return new Result();
    }

    @ApiOperation("删除 角色")
    @Throttle
    @RequestMapping(value = "/delete/{roleId}", method = RequestMethod.GET)
    public Result deleteRole(@PathVariable("roleId") String roleId) {
        roleService.deleteRole(roleId);
        return new Result();
    }

    @ApiOperation("更新 角色")
    @Throttle
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result updateRole(@RequestBody RoleSaveDTO roleSaveDTO) {
        roleService.updateRole(roleSaveDTO);
        return new Result();
    }

    @ApiOperation("根据 角色id 查询")
    @Throttle
    @RequestMapping(value = "/select/{roleId}", method = RequestMethod.GET)
    public Result selectRole(@PathVariable("roleId") String roleId) {
        RoleDTO role = roleService.selectRole(roleId);
        return new Result().data("role", role);
    }

    @ApiOperation("分页 条件 查询")
    @Throttle
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result listRole(@RequestParam(defaultValue = "1") Integer current,
                           @RequestParam(defaultValue = "30") Integer size,
                           RoleSearchDTO roleSearchDTO) {
        Page<RoleDTO> roles = roleService.listRole(current, size, roleSearchDTO);
        return new Result().data("roles", roles);
    }

    @ApiOperation("角色绑定菜单")
    @Throttle
    @RequestMapping(value = "/bind", method = RequestMethod.POST)
    public Result roleBindMenu(@RequestBody RoleMenuDTO roleMenuDTO) {
        roleService.bindRoleMenu(roleMenuDTO.getRoleId(), roleMenuDTO.getMenuIds());
        return new Result();
    }

}
